home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8273 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: newsstand.cit.cornell.edu!ub!newserve!rebecca!rpi!not-for-mail
  2. From: gstern@earth.usa.net (Greg Sternberg/PDS Inc)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: enum expression is IF statement
  5. Followup-To: comp.lang.c++.moderated,comp.lang.c++
  6. Date: 16 Feb 1996 11:25:29 -0000
  7. Organization: Internet Express (800-592-1240 customer service)
  8. Sender: cppmods@netlab.cs.rpi.edu
  9. Approved: devitto@ferndown.ate.slb.com
  10. Message-ID: <4g1pj9$pqg@netlab.cs.rpi.edu>
  11. References: <4fq7dc$pac@netlab.cs.rpi.edu>
  12. Reply-To: news@shiva.usa.net
  13. NNTP-Posting-Host: netlab.cs.rpi.edu
  14. X-Original-Date: 16 Feb 1996 06:30:31 GMT
  15.  
  16. Peter van Klaveren (P.v.Klaveren@bcs.cs.philips.com) wrote:
  17. : enum
  18. : {
  19. :    CSEQ_IDLE,
  20. :    CSEQ_ERROR,
  21. :    CSEQ_ABORT,
  22. :    CSEQ_WAIT4RESPONSE
  23. : };
  24.  
  25. : int state = 3;
  26.  
  27. : if (state == CSEQ_WAIT4RESPONSE)
  28. : {
  29. :     // this code is not executed!!!
  30. :     ...
  31. : }
  32.  
  33.    You're making the assumption that the compiler is numbering your enums
  34.    sequentially from 0 - since the standard says nothing about how the
  35.    compiler could number your enums it could very well be using 0x1, 0x2, 
  36.    0x4, and 0x8.
  37.  
  38. : BOOL expr_is_valid = state == CSEQ_WAIT4RESPONSE;
  39.  
  40. : if (expr_is_valid)
  41. : {
  42. :     // this code IS executed!!!
  43. :     ...
  44. : }
  45.  
  46. : I know it is not recommended to compare integers with enumeration
  47. : values, but why on earth is 'expr_is_valid' TRUE but seems
  48. : 'state == CSEQ_WAIT4RESPONSE' to result in FALSE (at least in a if
  49. : expression calcuation)?
  50.  
  51.    If I remember the leeway in the standard about enums was allowed so 
  52.    the compiler could optimize enums in any fashion it choose (i.e. bit
  53.    fields or char vs int)
  54. { If this is true this is a fairly obscure, important C incompatibility
  55.   references anyone ? -mod}
  56.  
  57. : Anybody an idea?
  58.  
  59. : Peter.
  60. :                                              \\\//
  61. :                                             -(o o)-
  62. : ==========================================ooO-(_)-Ooo======
  63. : Peter van Klaveren, E-mail: P.v.Klaveren@bcs.cs.philips.com
  64. : ===========================================================
  65.  
  66. :       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  67. :       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  68. :       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  69. :       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  70.  
  71. --
  72. CompuServ: 75511,65          "I don't know - I'm making this up as I go along." 
  73. Internet: gstern@usa.net     Indiana Jones - Raiders of the Lost Ark
  74.  
  75.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  76.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  77.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  78.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  79.